package com.itheima.controller;

import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.BlogSortService;
import com.itheima.vo.BlogSortVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/blogSort")
@Api(value = "后台管理模块", description = "博客分类相关接口")
public class BlogSortController {

    @Autowired
    private BlogSortService blogSortService;

    /**
     * 增加博客分类
     *
     * @param blogSortVO
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody BlogSortVO blogSortVO) {
        try {
            blogSortService.add(blogSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "添加失败");
        }

        return new Result("success", "添加成功");
    }

    /**
     * 批量删除博客分类
     *
     * @param blogSortVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody List<BlogSortVO> blogSortVoList) {
        try {
            for (BlogSortVO blogSortVO : blogSortVoList) {
                blogSortService.deleteBatch(blogSortVO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "批量删除失败");
        }

        return new Result("success", "批量删除成功");
    }

    /**
     * 编辑博客分类
     *
     * @param blogSortVO
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody BlogSortVO blogSortVO) {
        try {
            blogSortService.edit(blogSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "编辑失败");
        }

        return new Result("success", "编辑成功");
    }

    /**
     * 获取博客分类列表 根据条件进行分页查询
     *
     * @param blogSortVO
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody BlogSortVO blogSortVO) {
        PageResult pageResult = blogSortService.findAll(blogSortVO.getKeyword(), blogSortVO.getCurrentPage(), blogSortVO.getPageSize());
        return new Result("success", pageResult);
    }

    /**
     * 置顶分类
     *
     * @param blogSortVO
     * @return
     */
    @PostMapping("/stick")
    @ResponseBody
    public Result stickTag(@RequestBody BlogSortVO blogSortVO) {
        try {
            blogSortService.stick(blogSortVO);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "置顶失败");
        }

        return new Result("success", "置顶成功");
    }


    /**
     * 通过引用量排序博客分类
     *
     * @return
     */
    @PostMapping("/blogSortByCite")
    @ResponseBody
    public Result getBlogSortByCite() {
        try {
            blogSortService.blogSortByCite();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "排序失败");
        }
        return new Result("success", "排序成功");
    }


    /**
     * 通过点击量排序博客分类
     *
     * @return
     */
    @PostMapping("/blogSortByClickCount")
    @ResponseBody
    public Result getBlogSortByClickCount() {
        try {
            blogSortService.blogSortByClickCount();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result("error", "排序失败");
        }
        return new Result("success", "排序成功");
    }
}

